package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.OperationDataStatistics;
import com.itheima.health.service.ReportService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据报表service
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public OperationDataStatistics getOperationDataStatistics() {
        OperationDataStatistics operationDataStatistics = new OperationDataStatistics();
        /*获取会员总数*/
        Integer count = memberDao.getMemberCount();
        operationDataStatistics.setTotalMember(count);
        /*获取系统时间当天创建的会员数*/
        Date date = new Date();
        String patter1 = "yyyyMMdd";
        //返回今天新创建的会员数
        SimpleDateFormat sdf1 = new SimpleDateFormat(patter1);
        String today = sdf1.format(date);
        Integer todayNewMember = memberDao.getMemberTodayNewCount(today);
        operationDataStatistics.setTodayNewMember(todayNewMember);
        /*返回当天时间*/
        String patter2 = "yyyy-MM-dd";
        SimpleDateFormat sdf2 = new SimpleDateFormat(patter2);
        String format = sdf2.format(date);
        operationDataStatistics.setReportDate(format);
        /*返回当月一日至今的新增会员*/
        //创建一个Calendar对象
        Calendar calendar = Calendar.getInstance();
        //setTime(Date date)                   用给定的日期设置Calendar的当前时间
        calendar.setTime(date);
        //设置时间为当月1号
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String monthFirstDay = sdf1.format(calendar.getTime());
        //获取当月一日至今的新增会员个数
        Integer monthNewMember = memberDao.getDayToDayNewMember(monthFirstDay, today);
        operationDataStatistics.setThisMonthNewMember(monthNewMember);
        /*查询本周的新增会员*/
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        //  calendar.getFirstDayOfWeek()根据前面的设置 来动态的改变此值
        calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - day);
        String monday = sdf1.format(calendar.getTime());
        Integer weekNewMember = memberDao.getDayToDayNewMember(monday, today);
        operationDataStatistics.setThisWeekNewMember(weekNewMember);

        /*查询今日预约数*/
        Integer todayOrderCount = orderDao.getTodayOrderCount(today);
        operationDataStatistics.setTodayOrderNumber(todayOrderCount);
        /*本周预约数*/
        Integer weekOrderCount = orderDao.getDayToTodayOrderCount(monday, today);
        operationDataStatistics.setThisWeekOrderNumber(weekOrderCount);
        /*本月预约数*/
        Integer monthOrderCount = orderDao.getDayToTodayOrderCount(monthFirstDay, today);
        operationDataStatistics.setThisMonthOrderNumber(monthOrderCount);

        /*今日到诊数*/
        Integer todayVisitsCount = orderDao.getTodayVisitsNumber(today);
        operationDataStatistics.setTodayVisitsNumber(todayVisitsCount);
        /*本周到诊数*/
        Integer weekVisitsCount = orderDao.getDayToTodayVisitsCount(monday, today);
        operationDataStatistics.setThisWeekVisitsNumber(weekVisitsCount);
        /*本月到诊数*/
        Integer monthVisitsCount = orderDao.getDayToTodayVisitsCount(monthFirstDay, today);
        operationDataStatistics.setThisMonthVisitsNumber(monthVisitsCount);

        /*热门套餐名称*/
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        operationDataStatistics.setHotSetmeal(hotSetmeal);
        return operationDataStatistics;
    }

}
